<?php
namespace app\admin\controller;
use think\Controller;
use think\Request;
/**
 * Created by PhpStorm.
 * User: 空房间
 * Date: 2018/2/19
 * Time: 16:06
 */
class Supplier extends Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->db=new \app\admin\model\Supplier;
    }
    public function supplier_list(){
        $info = $this->db->supplier_list();
        $this->assign('info',$info);
        return view();
    }
    /**
     * 供应商添加
     */
    public function supplier_add()
    {
        if(Request::instance()->isPost()){
            $data = input('post.');
            if(!empty($data)){
                $inser_info = array(
                    'hospital_id'=>23,
                    'create_time'=>time(),
                    'update_time'=>time()
                );
                $data = array_merge($data,$inser_info);
                $result = $this->db->supplier_add($data);
                $result ? $this->success('成功') : $this->error('失败');
            }
        }
        return view();
    }
    /**
     * 供应商修改
     */
    public function supplier_edit()
    {
        if(Request::instance()->isPost()){
            $data = input('post.');
            if(!isset($data['sid'])||!$data['sid']){
                $this->error('参数不全操作失败');die;
            }
            $inser_info = array(
                'hospital_id'=>23,
                'update_time'=>time()
            );
            $data = array_merge($data,$inser_info);
            $result = $this->db->update_info($data['sid'],$data);
            $result ? $this->success('成功') : $this->error('失败');
        }
        $id = input('get.id');
        $info = $this->db->getInfoById(array('sid'=>$id));
        $this->assign('info',$info);
        return view();
    }

    /**
     * 供应商删除
     */
    public function supplier_del()
    {
        $id=input('post.id',0,'intval');
        if(!$id){
            $result = array(
                'status' =>0,
                'message'=>'参数错误，无法删除'
            );
        }else{
            $re=$this->db->del_info($id);
            if(!$re){
                $result = array(
                    'status' =>0,
                    'message'=>'删除失败'
                );
            }else{
                $result = array(
                    'status' =>1,
                    'message'=>'删除成功'
                );
            }
        }
        echo json_encode($result);
    }
}